package cn.wolfcode.trip.app.filter;

import cn.wolfcode.trip.base.util.UploadUtil;
import org.apache.commons.io.FileUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

public class ImageFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //强转
        HttpServletRequest req= (HttpServletRequest) servletRequest;
        //获取当前访问的图片路径
        String uri = req.getRequestURI();
        //判断当前区域是否有此图片
        File file = new File(UploadUtil.PATH, uri);
        if(file.exists()){
            //将图片响应给浏览器
            servletResponse.getOutputStream().write(FileUtils.readFileToByteArray(file));
        }else{
            filterChain.doFilter(servletRequest, servletResponse);
        }
    }
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}
    @Override
    public void destroy() {}
}
